package com.zzf.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzf.commonutils.JwtUtils;
import com.zzf.commonutils.R;
import com.zzf.eduorder.entity.Order;
import com.zzf.eduorder.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-22
 */

@EnableDiscoveryClient
@EnableFeignClients
@RestController
@RequestMapping("/eduorder/order")
@Api(description = "订单")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;


    //生成订单
    @PostMapping("createOrder/{courseId}")
    public R saveorder(@PathVariable String courseId, HttpServletRequest request) {

        //创建订单,返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("orderId", orderNo);
    }

    //根据订单ID查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);

        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

    //根据用户id和订单id查询订单信息
    @GetMapping("isBuyCourse/{memberId}/{courseId}")
    public boolean isBuyCourse(@PathVariable String memberId,
                               @PathVariable String courseId) {
        //订单状态是1表示支付成功
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId).
                eq("member_id", memberId).
                eq("status", 1);
        int count = orderService.count(wrapper);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}

